/**
 * Project Name:StudentHandler.java
 * File Name:StudentHandler.java
 * Date:2019-03-04 22:17
 * Copyright (c) 2019, zhang.xiangyu@foxmail.com All Rights Reserved.
 */
package com.meatball.student.handler;

import com.meatball.dispenser.client.DispenserClient;
import com.meatball.student.entity.Student;
import com.meatball.student.repository.StudentRepository;
import com.meatball.utils.MeatballUtils;
import com.meatball.utils.response.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * @Title: StudentHandler.java
 * @Description: todo(业务层)
 * @Author: 張翔宇
 * @Date: 2019-03-04 22:17
 * @Version: V1.0
 */
@Component
public class StudentHandler {
    private final StudentRepository studentRepository;

    private final DispenserClient dispenserClient;

    public StudentHandler (StudentRepository studentRepository, DispenserClient dispenserClient) {
        this.studentRepository = studentRepository;
        this.dispenserClient = dispenserClient;
    }

    /**
     * @title: selectStudent
     * @description: todo(获取所有用户)
     * @params: [request]
     * @return: reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>    返回类型
     * @author: 張翔宇
     * @date: 2019/3/5 15:03
     */
    public Mono<ServerResponse> selectStudent (ServerRequest request) {
        Student student = MeatballUtils.mapToObject(request.queryParams().toSingleValueMap(), Student.class);
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("name", matcher -> matcher.contains())
                .withMatcher("phone", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnorePaths("id"); // 不参与查询
        ExampleMatcher.PropertySpecifiers p =  exampleMatcher.getPropertySpecifiers();
        p.getSpecifiers().stream().map(item -> {
            System.out.println(item.getStringMatcher());
            System.out.println(item.getPropertyValueTransformer());
            return null;
        }).count();
        Example<Student> example = Example.of(student, exampleMatcher);
//        return ResultUtils.response(10000, "成功", studentRepository.findAll(example));
        Mono<Map> result = dispenserClient.getDispenserData();
        return ResultUtils.response(result);
    }

    /**
     * @title: selectById
     * @description: todo(根据ID查询学生)
     * @params: [request]
     * @return: reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>    返回类型
     * @author: 張翔宇
     * @date: 2019/3/5 21:40
     */
    public Mono<ServerResponse> selectById(ServerRequest request) {
        String id = request.pathVariable("id");
        return ResultUtils.response(10000, "成功", studentRepository.findById(id));
    }

    /**
     * @title: insertStudent
     * @description: todo(新增学生)
     * @params: [request]
     * @return: reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>    返回类型
     * @author: 張翔宇
     * @date: 2019/3/5 21:36
     */
    @Transactional
    public Mono<ServerResponse> insertStudent(ServerRequest request) {
        Mono<Student> studentMono = request.bodyToMono(Student.class);
        return ResultUtils.response(10000, "成功", studentRepository.insert(studentMono));
    }
}
